package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.gamestore.Bean.Admin;
import com.example.gamestore.Bean.Game;
import com.example.gamestore.Bean.User;
import com.example.gamestore.aop.AdminLogAnnotation;
import com.example.gamestore.mapper.AdminMapper;
import com.example.gamestore.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AdminUserControl {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;
    @GetMapping("/admin/user")
    public String adminuser(Model model, @RequestParam(name = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,5);
        List<User> users = userMapper.selectList(null);
        PageInfo<User> info=new PageInfo<>(users,5);
        model.addAttribute("pages",info);
        return "/Admin/user";
    }
    @PostMapping("/admin/userstatus")
    @AdminLogAnnotation(module = "用户模块",operater = "封解禁用户")
    @ResponseBody
    public String status(Integer id){
        User user = userMapper.selectById(id);
        User updatestatus = updatestatus(user);
        LambdaUpdateWrapper<User> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId,id);
        userMapper.update(updatestatus,wrapper);
        return "成功";
    }
    @GetMapping("/admin/root")
    @AdminLogAnnotation(module = "管理员模块",operater = "查看管理员信息")
    public String status(Model model){
        List<Admin> admins = adminMapper.selectList(null);
        model.addAttribute("list",admins);
        return "/Admin/root";
    }

    /**
     * 用户的封禁与解封
     * @param user
     * @return
     */
    public User updatestatus(User user){
        Integer status = user.getStatus();
        if(status==1){
            user.setStatus(0);
        }else {
            user.setStatus(1);
        }
        return user;
    }
}
